package com.cskaoyan.controller.vx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.vxBo.MobileBo;
import com.cskaoyan.bean.bo.vxBo.PasswordResetBo;
import com.cskaoyan.bean.bo.vxBo.VxRegisterBo;
import com.cskaoyan.service.vx.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: DYQ
 * @time: Created on 2022/6/8 14:03
 */

@RestController
@RequestMapping("wx/auth")
public class VxAuthController {

    String msg;
    //
//    @Autowired
//    HttpSession session;
////    @Autowired
//    HttpServletRequest request;
    @Autowired
    AuthService authService;

    @RequestMapping("regCaptcha")
    public BaseRespVo msgSend(@RequestBody MobileBo mobileBo) {



        if (!StringUtils.isNumeric(mobileBo.getMobile()) || mobileBo.getMobile().length() != 11) {
            return BaseRespVo.err("参数值不对", 402);
        } else {
            try {
                msg = authService.sendMsg(mobileBo.getMobile());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return BaseRespVo.ok(null);
        }
    }

    @RequestMapping("register")
    public BaseRespVo register(@RequestBody VxRegisterBo registerBo, HttpServletRequest request) {


        List<String> userNames = authService.getUserName();
        List<String> authServiceMobile = authService.getMobile();
        for (String mobile : authServiceMobile) {
            if (mobile.equals(registerBo.getMobile())) {
                return BaseRespVo.err("该手机号已经注册!", 705);
            }
        }
        for (String userName : userNames) {
            if (userName.equals(registerBo.getUsername())) {
                return BaseRespVo.err("用户名已注册!", 704);
            }
        }
        if (!StringUtils.isNumeric(registerBo.getMobile()) || registerBo.getMobile().length() != 11) {
            return BaseRespVo.err("手机号码格式不正确!", 707);
        } else if (!registerBo.getCode().equals(msg)) {
            return BaseRespVo.err("验证码错误!", 703);
        } else {
            authService.addUser(registerBo, request);
            return BaseRespVo.err("成功", 0);
        }
    }

    @RequestMapping("reset")
    public BaseRespVo reset(@RequestBody PasswordResetBo passwordResetBo) {

        List<String> authServiceMobile = authService.getMobile();
        if (!StringUtils.isNumeric(passwordResetBo.getMobile()) || passwordResetBo.getMobile().length() != 11) {
            return BaseRespVo.err("手机号码格式不正确!", 707);
        }
        else if (!passwordResetBo.getCode().equals(msg)) {
            return BaseRespVo.err("验证码错误!", 703);
        }
        for (String mobile : authServiceMobile) {
            if (passwordResetBo.getMobile().equals(mobile)) {
                authService.resetPassword(passwordResetBo);
                return BaseRespVo.ok(null);
            }
        }
        return BaseRespVo.err("手机号码未注册!", 701);
    }


}
